﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using Point = System.Drawing.Point;
using System.Windows.Forms;

using DotSpatial.Controls ;

using DotSpatial.Data;
using DotSpatial.Topology;


namespace HaGisPack
{
   public  class CircleMF : FMapFunctionBase 
   {


       # region 字段

     
       private List<Coordinate> _coordinates;
       //private List<List<Coordinate>> _parts;
       private bool _standBy;
       private System.Drawing.Point _mousePosition;
             
       # endregion


   




       # region 构造函数

       public CircleMF()
       {
           Configure();
       }

       public CircleMF(IMap map)
           :base(map)
       {
           Configure();
       }


       public void Configure()
       {
           //_parts = new List<List<Coordinate>>();       
       }
              
       # endregion


       # region 方法

       protected override void OnActivate()
       {
           if (_coordinates == null) _coordinates = new List<Coordinate>();




           _standBy = false;
           base.OnActivate();       
       
       }

       protected override void OnDeactivate()
       {
           if (_standBy) return;
           _standBy = true;


           
       }

       /// <summary>
       /// 根椐圆心和另一点求圆上所有的点
       /// </summary>
       /// <param name="Pt"></param>
       /// <returns></returns>
       private List<Coordinate> GetCirclePoints(List<Coordinate> Pt)
       {
           int Num = 360; //圆上点的个数

           List<Coordinate> CirclePoints = new List<Coordinate>(); //圆上的点

           double r = Math.Sqrt((Pt[1].Y - Pt[0].Y) * (Pt[1].Y - Pt[0].Y) + (Pt[1].X - Pt[0].X) * (Pt[1].X - Pt[0].X)); //半径

           for (int i = 0; i < Num; i++)
           {
               Coordinate tempt = new Coordinate();

               double Angle;
               Angle = 2 * Math.PI * i / Num;

               tempt.X = Pt[0].X + r * Math.Cos(Angle);
               tempt.Y = Pt[0].Y + r * Math.Sin(Angle);

               CirclePoints.Add(tempt);

           }

           return CirclePoints;

       }



       protected override void OnDraw(MapDrawArgs e)
       {
           if (_standBy) return;


           //---------------------------------------------

           //绘制和填充Part部分……


           //----------------------------------------------


               


           Pen redPen = new Pen(Color.Red, 3F);
           List<PointF> points = new List<PointF>();
           e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

           if (_coordinates != null && _coordinates.Count ==1)
           {
               points.Add(Map.ProjToPixel(_coordinates[0]));
               points.Add(_mousePosition);


               //List<PointF> CirclePoints = this.GetCirclePoints(points);

               float r = (float)Math.Sqrt(Convert.ToDouble((points[1].Y - points[0].Y) * (points[1].Y - points[0].Y) + (points[1].X - points[0].X) * (points[1].X - points[0].X))); //半径,要转两次类型！！！！！！！

               e.Graphics.DrawEllipse(redPen, points[0].X - r, points[0].Y - r, r * 2, r * 2);
                                        
           }
           redPen.Dispose();
           base.OnDraw(e);
       
       }


       protected override void OnMouseMove(GeoMouseArgs e)
       {
           if (_standBy) return;

           List<PointF> points = new List<PointF>();

           if (_coordinates != null && _coordinates.Count == 1)
           {
               points.Add(Map.ProjToPixel(_coordinates[0]));
               points.Add(_mousePosition);

               float r = (float)Math.Sqrt(Convert.ToDouble((points[1].Y - points[0].Y) * (points[1].Y - points[0].Y) + (points[1].X - points[0].X) * (points[1].X - points[0].X))); //半径,要转两次类型！！！！！！！

               Rectangle invalid;

               PointF tempt = new PointF();
               tempt.X = points[0].X - r;
               tempt.Y = points[0].Y - r;

               invalid = new Rectangle(Point.Round(tempt), new Size(Convert.ToInt32(r * 2), Convert.ToInt32(r * 2)));

               invalid.Inflate(20, 20);
               Map.Invalidate(invalid);           
           
           }

           _mousePosition = e.Location;
           base.OnMouseMove(e);

                     
       }

       protected override void OnMouseUp(GeoMouseArgs e)
       {
           if (_standBy) return;
           if (_coordinates == null) _coordinates = new List<Coordinate>();
           if (e.Button == MouseButtons.Left)
           {
               if (_coordinates.Count < 2)
               {
                   _coordinates.Add(e.GeographicLocation);
               }
           }
           else
           {
               _coordinates.Clear();
               Map.Refresh();
               
           }

           if (_coordinates.Count == 2) //画完后再做一次擦除工作
           {
               List<PointF> points = new List<PointF>();
               points.Add(Map.ProjToPixel(_coordinates[0]));
               points.Add(Map.ProjToPixel(_coordinates[1]));

               float r = (float)Math.Sqrt(Convert.ToDouble((points[1].Y - points[0].Y) * (points[1].Y - points[0].Y) + (points[1].X - points[0].X) * (points[1].X - points[0].X))); //半径,要转两次类型！！！！！！！

               Rectangle invalid;

               PointF tempt = new PointF();
               tempt.X = points[0].X - r;
               tempt.Y = points[0].Y - r;

               invalid = new Rectangle(Point.Round(tempt), new Size(Convert.ToInt32(r * 2), Convert.ToInt32(r * 2)));

               invalid.Inflate(20, 20);
               Map.Invalidate(invalid);

               //完成绘制

               FinishShape();
           
           }
       
       
       
       
       }

       int CircleID = 1;

       private void FinishShape()
       {
           //List<Coordinate> CirclePoints = new List<Coordinate>();
           //CirclePoints = this.GetCirclePoints(_coordinates); //求圆上的点

           double r = Math.Sqrt((_coordinates[0].X - _coordinates[1].X) * (_coordinates[0].X - _coordinates[1].X) + (_coordinates[0].Y - _coordinates[1].Y) * (_coordinates[0].Y - _coordinates[1].Y));

           foreach (IFeature f in (this.Parent.Parent.LayerSys.CurrentLayer.DataSet as IFeatureSet).Features)
           {
               CircleID = 1;
               if (CircleID <= int.Parse(f.DataRow["id"].ToString().Substring(1)))
               {
                   CircleID = int.Parse(f.DataRow["id"].ToString().Substring(1)) + 1;
               }
           }

           string id = "C" + CircleID.ToString();

           FCircleShape cir = new FCircleShape(r, _coordinates[0], id);
           this.OnShapeAdded(cir); //添加图形
           //CircleID++;

           _coordinates = new List<Coordinate>();  //清空

           //========================================================================
           


       
       }

       protected override void OnMouseWheel(GeoMouseArgs e)
       {
           base.OnMouseWheel(e);

           Rectangle r = e.Map.MapFrame.View;
           int w = r.Width;
           int h = r.Height;

           int scale = 5;

           if (e.Delta < 0)
           {
               r.Inflate(r.Width / (2 * scale), r.Height / (scale * 2));
               r.X += w / (2 * scale) - e.X / (scale);
               r.Y += h / (2 * scale) - e.Y / (scale);
               e.Map.MapFrame.View = r;
               e.Map.MapFrame.ResetExtents();
           }
           else
           {
               r.Inflate(-r.Width / (4 * scale), -r.Height / (4 * scale));
               // The mouse cursor should anchor the geographic location during zoom.
               r.X += (e.X / (2 * scale)) - w / (4 * scale);
               r.Y += (e.Y / (2 * scale)) - h / (4 * scale);
               e.Map.MapFrame.View = r;
               e.Map.MapFrame.ResetExtents();
           }

       }


       # endregion










   }
}
